//
//  OAuthViewController.swift
//  微博三天
//
//  Created by jessica on 15/11/13.
//  Copyright © 2015年 jessica. All rights reserved.
//

import UIKit
//添加网络框架备用
import AFNetworking
//网路监测,缓冲视图
import SVProgressHUD

//  添加网络接口白名单xcode7 ,在登录监听按钮的那里修改一下,进行转跳

class OAuthViewController: UIViewController {
    
    
//    App Key：
//    1931191387
//    App Secret：
//    4ef3a95ceb7cf7e85df656f650ab0150
//    网址:http://taobao.com
    
//    这部分写在Tools里可以了
    let client_id = "1931191387"
    let redirect_uri = "http://taobao.com"
    let client_scerct = "4ef3a95ceb7cf7e85df656f650ab0150"
    
    //定义一个webView
    let webView = UIWebView()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
        loadOauthPage()
        
    }
    private func setupUI(){
        
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "关闭", style: .Plain, target: self, action: "close")
        
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "自动填充", style: .Plain, target: self, action: "fillUp")
        
    }

    //实现方法,没有@objc 就会崩溃的, 要么把private 也去掉
  @objc  private func close (){
    
    dismissViewControllerAnimated(true, completion: nil)
    
    }
//    
    @objc private func fillUp (){
    
    let String = "document.getElementById('userId').value = '15089400649' , document.getElementById('passwd').value = 'fulin83805562'"
        
     webView.stringByEvaluatingJavaScriptFromString(String)
    }
    
    //网络窗口
    override func loadView() {
        super.loadView()
        
        view = webView
        webView.delegate = self
    }

//    https://api.weibo.com/oauth2/authorize
    
    
  private  func loadOauthPage (){
//        
//    +号两边一定要空格
        let urlString = "https://api.weibo.com/oauth2/authorize?" + "client_id=" + client_id + "&redirect_uri=" + redirect_uri

    
    let url = NSURL(string: urlString)
    let request = NSURLRequest(URL: url!)
    webView.loadRequest(request)
    
    }
}
extension OAuthViewController : UIWebViewDelegate{

    func webViewDidStartLoad(webView: UIWebView) {
        SVProgressHUD.show()
    }
    func webViewDidFinishLoad(webView: UIWebView) {
        SVProgressHUD.dismiss()
    }
    
//                                                               //request
    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
//        return true  这里是true 下面的就可以不用写了
        
        guard let urlString = request.URL?.absoluteString else{
        
        return false
        }
        
        if urlString.hasPrefix(redirect_uri){
        
        
        return true
            
        }
        
        if urlString.hasPrefix("https://api.weibo.com"){
        
        return true
        
        }
        //程序走到这里 一定会回调 url
        guard let query = request.URL?.query else {
        
        return false
        
        }
        
        let codeStr = "code="
        
        let code = query.substringFromIndex(codeStr.endIndex)
        
//          userAccountModel().loadAccessToken(code){(error)-> ()in
          userAccountViewModel().loadAccessToken(code) {(error)-> ()in
        
            print("come here")
      self.dismissViewControllerAnimated(true, completion: nil)
            
            //  按钮出现的时候发送通知
            NSNotificationCenter.defaultCenter().postNotificationName(AppSwitchRootViewControler, object: nil)
            

            
        }
       
        return false
    }
    

}


